package cn.lix.sc.rpc.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

/**
 * 代码生成器
 */
public class Generator {

    /** -------------------------------------  config start ----------------------------------------------*/
    private static final String _PROJECT_ROOT_PATH = "cn.lix.sc.rpc";

    // 请自己定义文件生成路径
    private static final String _ROOT_OUT_PUT_DIR = "C:/code_generator";

    private static final String _DRIVER   = "com.mysql.cj.jdbc.Driver";
    private static final String _URL      = "jdbc:mysql://127.0.0.1:3306/oauth2?useSSL=false&allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8";
    private static final String _USERNAME = "root";
    private static final String _PASSWORD = "lixiang88";

    // 请配置需要生成的表
    private static final String[] GEN_TABLES = new String[] {
            "oauth_access_token"
    };
    /** -------------------------------------  config end ----------------------------------------------*/


    public static void main(String[] args) {
        System.out.println("========开始执行=======");
        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setActiveRecord(false)
                .setOutputDir(_ROOT_OUT_PUT_DIR)
                .setAuthor("generator")
                .setEntityName("%s")
                .setServiceName("%sService")
                .setMapperName("%sMapper")
                .setFileOverride(false)
                .setEnableCache(false)
                .setActiveRecord(false)
                .setBaseResultMap(true)
                .setDateType(DateType.ONLY_DATE)
                .setBaseColumnList(false);

        //数据源配置
        DataSourceConfig dataSourceConfig =new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setDriverName(_DRIVER)
                .setUrl(_URL)
                .setUsername(_USERNAME)
                .setPassword(_PASSWORD);

        dataSourceConfig.setTypeConvert(new MySqlTypeConvert() {
            public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                System.out.println("转换类型：" + fieldType);
                if (fieldType.toLowerCase().contains("tinyint")) {
                    return DbColumnType.INTEGER;
                }
                return super.processTypeConvert(globalConfig, fieldType);
            }
        });

        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(false)
                .setInclude(GEN_TABLES);

        //包名策略配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(_PROJECT_ROOT_PATH)
                .setMapper("mapper")
                .setService("service")
                .setEntity("domain")
                .setXml("mapper");

        //整合配置
        AutoGenerator ag = new AutoGenerator();
        ag.setGlobalConfig(globalConfig)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig)
                .setTemplateEngine(new FreemarkerTemplateEngine());

        //执行
        ag.execute();
        System.out.println("========生成成功=======");
    }

}
